$(function(){
	$.ajax({
		type:'get',
		url:url() + '/userWithdraw/preWithdraw',
		data:{},
		dataType:"json",
		success:function(rel,sta,xhr){
			var sessionstatus = xhr.getResponseHeader('redirectStatus');
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
				return false;
			};
			var rel_obj = JSON.parse(rel.result);
			if(rel.code == 0 && rel_obj.status != 0){
				alert(rel_obj.message)
			};
			//console.log(JSON.parse(rel.result));
			/*用户全部可用余额*/
			var balance= rel_obj.object.userBalance.mainMoney + rel_obj.object.userBalance.bonusMoney + rel_obj.object.userBalance.platformBonus - rel_obj.object.userBalance.frozenMainMoney - rel_obj.object.userBalance.frozenBonusMoney - rel_obj.object.userBalance.frozenPlatformBonus;  
			/*邀请的人的返利*/
			var platMoney = rel_obj.object.userBalance.platformBonus - rel_obj.object.userBalance.frozenPlatformBonus;  
			$('#bal_money').text('余额：'+balance.toFixed(2)+'元');

			/*输入金额计算个税，手续费，实际到账*/
			$('.header input').bind('input propertychange',function(){
				var gs = 0,sxf = 0;
				if($(this).val() >= balance){
					$(this).val(balance);
				};
				/*个税*/
				if($(this).val() > platMoney){
					gs = parseFloat(platMoney*rel_obj.object.personTaxPercent/100);
					$('.form input').eq(0).val(gs.toFixed(2));
				}else{
					gs = parseFloat($(this).val()*rel_obj.object.personTaxPercent/100);
					$('.form input').eq(0).val(gs.toFixed(2));
				};
				/*手续费*/
				sxf = parseFloat($(this).val()*rel_obj.object.servicePercent/100);
				$('.form input').eq(1).val(sxf.toFixed(2));
				/*实际到账*/
				$('.form input').eq(2).val(($(this).val()-gs-sxf).toFixed(2));
				/*显示元字*/
				$('.form input').each(function(){
					$(this).parent().find('em').show();
					$(this).parent().find('em').css('left',$(this).val().length/2+ 1 + 'em');
				});
				/*激活按钮*/
				if($(this).val() != '' && $(this).val() > 0){
					$('.cash-btn button').removeClass('disabled').removeAttr('disabled')
				}else{
					$('.cash-btn button').addClass('disabled').attr('disabled','true');
				}
			});
			/*全部提现*/
			$('.all-cash').click(function(){
				var gs = 0,sxf = 0;
				$('.header input').val(balance.toFixed(2));
				/*个税*/
				if($('.header input').val() > platMoney){
					gs = parseFloat(platMoney*rel_obj.object.personTaxPercent/100);
					$('.form input').eq(0).val(gs.toFixed(2));
				}else{
					gs = parseFloat($('.header input').val()*rel_obj.object.personTaxPercent/100);
					$('.form input').eq(0).val(gs.toFixed(2));
				};
				/*手续费*/
				sxf = parseFloat($('.header input').val()*rel_obj.object.servicePercent/100);
				$('.form input').eq(1).val(sxf.toFixed(2));
				/*实际到账*/
				$('.form input').eq(2).val(($('.header input').val()-gs-sxf).toFixed(2));
				/*显示元字*/
				$('.form input').each(function(){
					$(this).parent().find('em').show();
					$(this).parent().find('em').css('left',$(this).val().length/2+ 1 + 'em');
				});
				/*激活按钮*/
				if(balance > 0){
					$('.cash-btn button').removeClass('disabled').removeAttr('disabled')
				}
			});
			/*确认提现按钮*/
			$('.cash-btn button').click(function(){
				if(rel_obj.object.hasIdCard == 0){
					window.location.href = url() + '/htm/cash-inform.html';
				}else if(rel_obj.object.hasPayPassword == 0){
					window.location.href = url() + '/htm/cash-password.html';
				}else{
					$('.cash-password').show();
					$('.error-tips').text('');
				}
			});
			/*点击阴影关闭支付弹窗*/
			$('.cash-password-bg').click(function(){
				$('.cash-password').hide();
			});
			/*提现输入提现密码确认按钮*/
			$('.cash-password button').click(function(){
				$.ajax({
					type:'post',
					url:url() + '/userWithdraw/confirmH5Withdraw',
					data:{
						totalMoney:$('.header input').val(),
						password:$('.cash-password input').val()
					},
					dataType:'json',
					success:function(res,sta,xhr){
						var sessionstatus = xhr.getResponseHeader('redirectStatus');
						
						if(sessionstatus == '302'){
							window.location.href = url() + '/wxuser/goCompleteInfo';
							return false;
						};
						var res_obj = JSON.parse(res.result);
						//console.log(res_obj);
						if(res.code == 0 && res_obj.status == 0){
							/*提现到账金额*/
							localStorage.cash_money = $('.form input').eq(2).val();
							window.location.href = url() + '/htm/cash-submit.html';
						}else if(res.code == 0 && res_obj.status == '-2'){
							$('.error-tips').text('密码错误');
						}else{
							alert(res_obj.message);
						}
					},
					error:function(){
						console.log('error')
					}
				})
			});
			/*个税和手续费的提示信息*/
			$('.tip-icon').each(function(){
				$(this).click(function(){
					$('.cash-prompt').show();
					if($(this).attr('data-fee') == 'ges'){
						$('.cash-prompt .ges').show();
						$('.cash-prompt .sxf').hide();
					}else{
						$('.cash-prompt .ges').hide();
						$('.cash-prompt .sxf').show();
					}
				})
			});
			$('.cash-prompt').click(function(){
				$(this).hide();
			})
		},
		error:function(){
			console.log('error')
		}
	})
})